package com.zjy.order.controller;

import com.zjy.order.service.OrderProductService;
import com.zjy.order.domain.OrderProduct;
import com.zjy.order.query.OrderProductQuery;
import com.zjy.basic.util.AjaxResult;
import com.zjy.basic.util.PageList;
import com.zjy.pet.dto.OrderDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderProduct")
public class OrderProductController {

    @Autowired
    public OrderProductService orderProductService;

    /**
     * 接口：添加或修改
     * @param orderProduct  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult save(@RequestBody OrderProduct orderProduct){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.add(orderProduct);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            orderProductService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
                orderProductService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderProduct findById(@PathVariable("id")Long id) {
        return orderProductService.findById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderProduct> findAll(){
        return orderProductService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<OrderProduct> queryPage(@RequestBody OrderProductQuery query, HttpServletRequest request) {
        return orderProductService.queryList(query, request);
    }

    /**
     * 确认订单
     * @param orderDto
     * @return
     */
    @PostMapping(value="/confirm")
    public AjaxResult confirm(@RequestBody OrderDto orderDto){
        try {
            orderProductService.confirm(orderDto);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }
}
